package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.Gift;
import cn.com.connext.scrm.repository.master.GiftRepository;
import cn.com.connext.scrm.service.GiftService;
import cn.com.connext.scrm.service.dto.GiftDTO;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.MemberDTO;
import cn.com.connext.scrm.web.rest.vm.UnionIdVM;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: organization-service
 * @author: shawn
 * @create: 2019-06-17 10:40
 **/
@RestController
@RequestMapping("/api/gift/")
public class GiftResource extends BaseResource {

    private final Logger log = LoggerFactory.getLogger(GiftResource.class);




    public GiftResource() {
        super("gift");
    }

    @Autowired
    private GiftService giftService;

    @PostMapping(value = "list")
    @Timed
    @ApiOperation(value = "礼物列表", tags = {"礼物管理"}, notes = "礼物列表")
    public List<GiftDTO> list(){
        try{
            List<GiftDTO> list = giftService.list();
            return list;
        }catch (Exception e){
            log.error("查询失败:{}", e);
            return null;
        }
    }

    @PostMapping(value = "binding")
    @Timed
    @ApiOperation(value = "用户绑定", tags = {"礼物管理"}, notes = "用户绑定")
    public ResponseEntity binding(@RequestBody UnionIdVM vm){
        JsonResult result = new JsonResult();
        try {
            giftService.bindind(vm.getUnionId());
            result.setMessage("绑定成功");
            result.setState(0);
            return entityResponse(result);
        }catch (Exception e){
            log.error("绑定失败:{}", e);
            result.setMessage("绑定失败");
            result.setState(1);
            return entityResponse(result);
        }
    }

}
